﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Domain.Models;
using System.Data;
using Utilities;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Domain.DAL
{
    public class RepairQualityDAO
    {
        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public RepairQualityDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public RepairQuality Get(int id)
        {
            RepairQuality theRepairQuality = new RepairQuality();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairQualities_Get", id);
                if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    theRepairQuality.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    theRepairQuality.RepairQualityName = ds.Tables[0].Rows[0]["RepairQualityName"].ToString();
                    theRepairQuality.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theRepairQuality;
        }

        public int Insert(RepairQuality theRepairQuality)
        {
            int result = MyConfig.NullNumber;

            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_RepairQualities_Insert", theRepairQuality.RepairQualityName));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool Update(RepairQuality theRepairQuality)
        {
            bool result = false;

            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_RepairQualities_Update", theRepairQuality.ID, theRepairQuality.RepairQualityName)) == theRepairQuality.ID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool Delete(string id)
        {
            bool result = false;

            try
            {
                result = this.db.ExecuteScalar("sp_RepairQualities_Delete", id.ToString()).ToString() == id;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public DataSet Search(RepairQuality theRepairQuality)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairQualities_List", theRepairQuality.ID, theRepairQuality.RepairQualityName);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        #endregion

        #region Public Static Methods

        public static RepairQualityDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<RepairQualityDAO>();
        }

        #endregion
    }
}
